package cn.gson.fengboot.model.service;

import cn.gson.fengboot.model.dao.*;
import cn.gson.fengboot.model.mapper.ZbarterMapper;
import cn.gson.fengboot.model.mapper.ZchukuMapper;
import cn.gson.fengboot.model.mapper.ZcontractMapper;
import cn.gson.fengboot.model.pojos.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ZbarterService {
    @Autowired
    ZbarterMapper hhdmapper;

    @Autowired
    ZbarterDao dao;
    
    @Autowired
    ZbarterdetailsDao xqdao;

    @Autowired
    ZchukuxqDao ckxqdao;

    @Autowired
    ZchukuDao ckdao;

    @Autowired
    ZchukuMapper ckmapper;

    @Autowired
    ZorderDao dddao;

    /*
        订单弹出框查询方法
     */
    public List<Zbarter> huanhuolist(String search, String search2){
        return hhdmapper.huanhuolist(search,search2);
    }

    /*
        订单换货单
     */
    public List<Zbarter> ddhhd(String search){
        return hhdmapper.ddhhd(search);
    }

    /*
        更具换货单ID查询换货单数据
     */
    public Zbarter ddxqcx(String search){
        return hhdmapper.ddxqcx(search);
    }

    /*
     换货单详情产品
    */
    public
    List<Zbarterdetails>allpro(String search){
        return hhdmapper.allpro(search);
    }

    /*
     换货单弹框
    */
    public List<Zbarter>hhtk(String search, String search2){
        return hhdmapper.hhtk(search,search2);
    }

    public void addhuanhuo(Zbarter hh){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());
        Timestamp ts = Timestamp.valueOf(time);
        hh.setBarterTime(ts);
        Zbarter save = dao.save(hh);

        //修改订单为已换过货
        Zorder zorder = dddao.findById(hh.getOrderId().getOrderId()).get();
        zorder.setOrderExchange("是");
        dddao.save(zorder);

//        剪掉出库单详情的可退换数量
        List<Zchukuxq> chukuxq = ckmapper.chukuxq(hh.getChukuId().getChukuId(),123);
        for (int i =0;i<hh.getZbarterdetailsByBarterId().size();i++) {
            for (int j =0;j<chukuxq.size();j++) {
                if(chukuxq.get(i).getProdetailId().getProdetailId()==hh.getZbarterdetailsByBarterId().get(j).getProdetailId().getProdetailId()){
                    chukuxq.get(i).setChukuKetueinumber(chukuxq.get(i).getChukuKetueinumber()-hh.getZbarterdetailsByBarterId().get(j).getBarterdetailsNumber());
                }
            }
        }
        ckxqdao.saveAll(chukuxq);



        //判断是否还有可退还数量
        Integer sfth = 0;
            for (int i =0;i<chukuxq.size();i++) {
                if(chukuxq.get(i).getChukuKetueinumber()!=0){
                    sfth=1;
                }
            }

        //更具判断修改出库单的是否退换货状态
        Zchuku zchuku = ckdao.findById(hh.getChukuId().getChukuId()).get();
        if (sfth==0){
            zchuku.setChukuShifouth("已退换");
        }else {
            zchuku.setChukuShifouth("部分退换");
        }
        ckdao.save(zchuku);

        //新增换货详情表数据
        hh.getZbarterdetailsByBarterId().forEach(pur->{
            pur.setBarterId(save);
        });
        xqdao.saveAll(hh.getZbarterdetailsByBarterId());
    }

    //<!--谭方圆=>客户合并的出库查询-->
    public List<Zbarter> tfyhuanhuolists(Integer id1, Integer id2){
        return hhdmapper.tfyhuanhuolists(id1,id2);
    }
}
